<style lang="scss">
	.add {
		padding: 10rpx;

		input {
			border: 1px solid #ccc;
			margin: 10px 0;
			height: 60rpx;
		}
	}
</style>

<template>
	<view class="add">
		<input type="text" placeholder="请输入标题" v-model="addData.title" />
		<input type="text" placeholder="请输入图片路径" v-model="addData.image" />
		<input type="number" placeholder="请输入价钱" v-model.number="addData.price" />
		<input type="text" placeholder="请输入描述" v-model="addData.desc" />
		<input type="text" placeholder="请输入类型" v-model="addData.type" />
		<input type="number" placeholder="请输入数量" v-model.number="addData.count" />
		<button @click="handleAdd">确定</button>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	const _id = ref('')
	const addData = ref({
		title: '',
		image: '',
		price: '',
		desc: '',
		type: '',
		count: '',
		state: false
	})


	const handleAdd = async () => {
		console.log(addData.value)
		if (_id.value) {
			// 编辑
			const resp = await uniCloud.database().collection('goodList').doc(_id.value).update(addData.value)
		} else {
			// 添加
			const resp = await uniCloud.database().collection('goodList').add(addData.value)
		}
		console.log('操作完成')
	}

	onLoad(async options => {
		if (options._id) {
			// 编辑
			uni.setNavigationBarTitle({
				title: '编辑'
			})

			const resp = await uniCloud.database().collection('goodList').doc(options._id).get({
				getOne: true
			});
			_id.value = options._id
			addData.value.title = resp.result.data.title
			addData.value.image = resp.result.data.image
			addData.value.desc = resp.result.data.desc
			addData.value.count = resp.result.data.count
			addData.value.price = resp.result.data.price
			addData.value.type = resp.result.data.type
		} else {
			// 添加
			uni.setNavigationBarTitle({
				title: '添加'
			})
		}
	})
</script>

<style lang="scss">

</style>